layui.config({
    base : "js/"
}).use(['form','layer','jquery','layedit','laydate'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    form.on('select(auth)', function(data){
        
        if(data.value == 0){

            $("#modn").removeClass("hide")
        }else{
                $("#modn").addClass("hide")
        }

    })
    form.on("switch(Spread)",function(data){
        if(data.elem.checked){
            $('input[name=spread]').val('1');
        }else{
            $('input[name=spread]').val('0');
        }
    })
  
    form.on("switch(Limit)",function(data){
        if(data.elem.checked){
            $('input[name=limit]').val('1');
        }else{
            $('input[name=limit]').val('0');
        }
    })

    form.on("switch(Gadget)",function(data){
        if(data.elem.checked){
            $('input[name=gadget]').val('1');
        }else{
            $('input[name=gadget]').val('0');
        }
    })

    form.on('submit(addAuth)',function(data){

        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type:'post',
            url:'/admin/auth/store',
            async:false,
            data:{
                '_token':$('input[name=_token]').val(),
                'title':$('.title').val(),
                'icon_id':$('#icon').val(),
                'href':$('.href').val(),
                'auth_id':$('#auth').val(),
                'module':$('.mod').val(),
                'spread':$('input[name=spread]').val(),
                'limit':$('input[name=limit]').val(),
                'gadget':$('input[name=gadget]').val()
            },
            dataType:'json',
            success:function (data) {
                if(data.status == 1){
                    top.layer.close(index);
                    top.layer.msg("权限添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }else{
                    top.layer.close(index);
                    top.layer.msg("权限添加失败！");
                }
            }
        })
        return false;
    })

})
